package com.ggh.jinjilive.dialog.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.amap.api.services.core.PoiItem;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemNearInformationBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;

import java.util.List;

public class NearInformationAdapter extends RecyclerView.Adapter<NearInformationAdapter.ItemViewHolder> {
    private List<PoiItem> poiItemList;


    public void setAdd(List<PoiItem> poiItemList){
        this.poiItemList = poiItemList;
        notifyDataSetChanged();
    }

    private AddItemOnClickListener addItemOnClickListener;
    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener){
        this.addItemOnClickListener = addItemOnClickListener;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemNearInformationBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_near_information, parent, false);
        return new ItemViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        PoiItem poiItem = poiItemList.get(position);
        if(null != poiItem){
            holder.binding.tvTitle.setText(poiItem.getTitle());
            if(poiItem.getDistance() > 0){
                holder.binding.tvLocation.setVisibility(View.VISIBLE);
                holder.binding.viewLine.setVisibility(View.VISIBLE);
                holder.binding.tvLocation.setText(poiItem.getDistance() +"m");
            }else {
                holder.binding.tvLocation.setVisibility(View.GONE);
                holder.binding.viewLine.setVisibility(View.GONE);
            }

            holder.binding.tvContent.setText(poiItem.getSnippet());
            holder.binding.llLocation.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(null != addItemOnClickListener){
                        addItemOnClickListener.onSkipClicked(position,poiItem);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return null == poiItemList ? 0 : poiItemList.size();
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        ItemNearInformationBinding binding;

        public ItemViewHolder(@NonNull ItemNearInformationBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}